package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.entity.guang.CarListEntity
import com.c.ctgproject.ui.collect.*
import com.c.ctgproject.utils.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource

//type 1:动力电池 散件录入 我的录入 2 电池搜索 3多选+状态
class BatteryApplyOutWarehouseAdapter(private val context: Context, private val mList: List<BatteryInWarehouseEntity.DataBean.ListBean>,private val listener:CallBack,private var isMine:Boolean) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_battery_apply_out_warehouse_list, parent, false)
        return MyViewHolder(view)
    }
    fun setIsMine(mine:Boolean){
        this.isMine=mine
    }
    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            if(isMine){
                holder.mCheckBoxLL.visibility=View.GONE
            }else{
                holder.mCheckBoxLL.visibility=View.VISIBLE
            }
            holder.mCheckBox.isChecked=data.checked
            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            holder.batteryCode.text = data.batteryCode.noDataHandler()
//            holder.carPlate.text = data.carPlate.noDataHandler()
            holder.batterySelfCode.text = data.batterySelfCode.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mCodeTV.text = data.orderNo.noDataHandler()
            holder.mBatteryNameTV.text = data.batteryName.noDataHandler()
            holder.mStatueTV.text = when (data.returnStatus) {
                "0" -> "审批中"
                "1" -> {if(data.status==0)"审批通过" else "重新入库"}
                "2" -> "驳回"
                else -> ""
            }

            val batteryType = when (data.batteryType) {
                1 -> "电池包"
                2 -> "模组"
                3 -> "电芯"
                else -> "暂无数据"
            }
            holder.mBatteryModelTV.text = batteryType
            holder.mBatteryCountTV.text = data.count.noDataHandler()
            holder.mBatteryFromTV.text = data.batterySource.noDataHandler()
            holder.carCode.text = data.carCode.handlerNull()
//
//
            holder.mBottomRL.setOnClickListener {
                holder.mBTN.performClick()
            }
            //详情
            holder.mParentLL.setOnClickListener {
                if (data.orderId == null) {
                    context.showToast("该电池暂无详情")
                } else {
                    //电池详情
                    context.startActivity<BatteryCanHandlerDetailActivity>(MyConstants.ID to data.pbId)
                }
            }
            holder.mCheckBoxLL.setOnClickListener {
                holder.mCheckBox.performClick()
                data.checked=holder.mCheckBox.isChecked
                listener.itemCheckSelect(position,data)
            }
        }
    }
    interface CallBack{
        fun itemCheckSelect(position:Int,data:BatteryInWarehouseEntity.DataBean.ListBean)
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        //        val carPlate: TextView = view.findViewById(R.id.carPlate)
        val batteryCode: TextView = view.findViewById(R.id.batteryCode)
        val mCodeTV: TextView = view.findViewById(R.id.mCodeTV)
        val batterySelfCode: TextView = view.findViewById(R.id.batterySelfCode)
        val mLine: View = view.findViewById(R.id.mLine)
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)

        //        val mStatueTV: TextView = view.findViewById(R.id.mStatueTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mBatteryNameTV: TextView = view.findViewById(R.id.mBatteryNameTV)
        val mBatteryModelTV: TextView = view.findViewById(R.id.mBatteryModelTV)
        val mBatteryCountTV: TextView = view.findViewById(R.id.mBatteryCountTV)
        val mBatteryFromTV: TextView = view.findViewById(R.id.mBatteryFromTV)
        val mBottomRL: RelativeLayout = view.findViewById(R.id.mBottomRL)
        val mBTN: Button = view.findViewById(R.id.mBTN)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mCheckBox: CheckBox = view.findViewById(R.id.mCheckBox)
        val carCode: TextView = view.findViewById(R.id.carCode)
        val mCheckBoxLL: LinearLayout = view.findViewById(R.id.mCheckBoxLL)
        val mStatueTV: TextView = view.findViewById(R.id.mStatueTV)
    }


}